package cn.ithers.blog.controller.admin;

import cn.ithers.blog.annotion.CheckTooFrequentCommit;
import cn.ithers.blog.dto.ArticleDTO;
import cn.ithers.blog.dto.UpdateArticleDTO;
import cn.ithers.blog.response.ResponseResult;
import cn.ithers.blog.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhanghuan
 * @date: 2020/8/28 14:06
 * @description:
 */

@Slf4j
@Api(value = "文章", tags = "文章")
@RestController
@RequestMapping("/admin/article")
public class ArticleAdminApi {

    @Autowired
    private IArticleService articleService;

    /**
     * 发表文章
     *
     * @param articleDTO
     * @return
     */

    @CheckTooFrequentCommit
    @PreAuthorize("@permission.admin()")
    @ApiOperation("发表文章")
    @PostMapping()
    public ResponseResult postArticle(@RequestBody ArticleDTO articleDTO) {
        return articleService.postArticle(articleDTO);
    }

    /**
     * 删除文章
     * 如果是多用户，用户不可以删除，删除只是修改状态
     * 管理员可以删除
     * 这里是真删除
     *
     * @param articleId
     * @return
     */
    @ApiOperation("删除文章(物理删除)")
    @PreAuthorize("@permission.admin()")
    @DeleteMapping("/{articleId}")
    public ResponseResult deleteArticle(@ApiParam(value = "文章ID", required = true) @PathVariable("articleId") String articleId) {
        return articleService.deleteArticleById(articleId);
    }

    /**
     * 修改文章
     *
     * @param articleId
     * @return
     */
    @CheckTooFrequentCommit
    @PreAuthorize("@permission.admin()")
    @ApiOperation("修改文章")
    @PutMapping("/{articleId}")
    public ResponseResult updateArticle(@ApiParam(value = "文章ID", required = true) @PathVariable("articleId") String articleId,
                                        @RequestBody UpdateArticleDTO updateArticleDTO) {
        return articleService.updateArticle(articleId, updateArticleDTO);
    }

    /**
     * 获取文章详情
     *
     * @param articleId
     * @return
     */
    @PreAuthorize("@permission.admin()")
    @ApiOperation("获取文章详情")
    @GetMapping("/{articleId}")
    public ResponseResult getArticle(@ApiParam(value = "文章ID", required = true) @PathVariable("articleId") String articleId) {
        return articleService.getArticleById(articleId);
    }

    /**
     * 获取文章列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @PreAuthorize("@permission.admin()")
    @ApiOperation("获取文章列表")
    @GetMapping("/list/{page}/{pageSize}")
    public ResponseResult listArticles(@ApiParam(value = "当前页码", required = true) @PathVariable("page") int page,
                                       @ApiParam(value = "每页显示条数", required = true) @PathVariable("pageSize") int pageSize,
                                       @ApiParam("按标题搜索") @RequestParam(value = "keywords", required = false) String keyword,
                                       @ApiParam("分类ID") @RequestParam(value = "categoryId", required = false) String categoryId,
                                       @ApiParam("文章状态") @RequestParam(value = "state", required = false) String state) {
        return articleService.articleList(page, pageSize, keyword, categoryId, state);
    }

    /**
     * 更新文章状态
     *
     * @param articleId
     * @return
     */
    @CheckTooFrequentCommit
    @PreAuthorize("@permission.admin()")
    @ApiOperation("删除文章通过改变状态")
    @PutMapping("/state/{articleId}")
    public ResponseResult deleteArticleByUpdateState(@ApiParam(value = "文章ID", required = true) @PathVariable("articleId") String articleId) {
        return articleService.deleteArticleByState(articleId);
    }

    /**
     * 文章置顶
     *
     * @param articleId
     * @return
     */
    @PreAuthorize("@permission.admin()")
    @ApiOperation("文章置顶")
    @PutMapping("/top/{articleId}")
    public ResponseResult updateArticleState(@ApiParam(value = "文章ID", required = true) @PathVariable("articleId") String articleId) {
        return articleService.topArticle(articleId);
    }
}
